package org.strongme.modules.project.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.strongme.common.service.CrudService;
import org.strongme.modules.project.dao.TzDao;
import org.strongme.modules.project.entity.Fy;
import org.strongme.modules.project.entity.Tz;

@Service
@Transactional(readOnly=true)
public class TzService extends CrudService<TzDao, Tz>{
	
	@Autowired
	private FyService fyService;
	
	
	/**
	 * 获取某单位工程下的所有管线号
	 * @param subProjectId
	 * @return
	 */
	public List<Tz> getDistinctGXBHListBySubProId(String subProjectId) {
		return dao.getDistinctGXBHListBySubProId(subProjectId);
	}
	
	/**
	 * 获取单位工程下所有规格
	 * @return
	 */
	public List<Tz> getDistinctGGListBySubProId(Map<String, Object> param) {
		return dao.getDistinctGGListBySubProId(param);
	}
	
	/**
	 * 根据单位工程 规格 管线号
	 * @param tz
	 * @return
	 */
	public List<Tz> findListBySub_GXH_GG(Tz tz) {
		return dao.findListBySub_GXH_GG(tz);
	}
	
	public List<Tz> findListByGXH(Tz tz) {
		return dao.findListByGXH(tz);
	}
	public Integer getPianshuByGXH(String gxh) {
		return dao.getPianshuByGXH(gxh);
	}
	
	public Integer countTzBySubGxh(Tz tz) {
		return dao.countTzBySubGxh(tz);
	}
	
	/**
	 * 检测此管线下是否已有指定焊口编号
	 * @param tz
	 * @return
	 */
	public Integer existHkInThisGx(Tz tz) {
		return dao.existHkInThisGx(tz);
	}
	
	@Transactional(readOnly=false)
	public void save(Tz tz) {
		//保存台账的同时插入附页数据
		if(tz!=null&&(tz.getId()==null||"".equals(tz.getId()))) {
			Integer ps = Integer.valueOf(tz.getPs());
			for(int i=0;i<ps;i++) {
				Fy fytmp = new Fy();
				fytmp.setTz(tz);
				fytmp.setProject(tz.getSubProject().getProject());
				fytmp.setSubProject(tz.getSubProject());
				fytmp.setJjmc(tz.getJjmc());
				fytmp.setJjbh(tz.getGxh());
				fytmp.setHkbh(tz.getHkbh());
				fytmp.setPh(String.valueOf(i+1));
				fytmp.setGg(tz.getGg());
				fytmp.setQxxz("未发现缺陷");
				fytmp.setQxdl("/");
				fytmp.setPdjg("1");
				fytmp.setHgh(tz.getHgh());
				fyService.save(fytmp);
			}
		}else {
			//更新
		}
		super.save(tz);
	}

	@Override
	@Transactional(readOnly=false)
	public void delete(Tz entity) {
		super.delete(entity);
		//同时删除相应附页数据
		Fy fy = new Fy();
		fy.setProject(entity.getSubProject().getProject());
		fy.setSubProject(entity.getSubProject());
		fy.setJjbh(entity.getGxh());
		fy.setHkbh(entity.getHkbh());
		fyService.deleteByGxhProSub(fy);
	}
	
}
